package cn.tedu.luban.order.main.repository.impl;
import cn.tedu.luban.order.main.converter.OrderConverter;
import cn.tedu.luban.order.main.dao.OrderDAO;
import cn.tedu.luban.order.main.repository.OrderRepository;
import cn.tedu.luban.order.protocol.bo.OrderBO;
import cn.tedu.luban.order.protocol.dos.OrderDO;
import cn.tedu.luban.order.protocol.param.OrderCreateParam;
import cn.tedu.luban.order.protocol.param.OrderParam;
import cn.tedu.luban.order.protocol.query.AdminOrderDBPagerQuery;
import cn.tedu.luban.order.protocol.query.AdminOrderListQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderDBPagerQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderListQuery;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired(required = false)
    private OrderDAO orderDAO;
    @Autowired
    private OrderConverter orderConverter;
    @Override public Long countOrders(AdminOrderListQuery adminOrderListQuery) {
        AdminOrderDBPagerQuery query = orderConverter.query2dbQuery(adminOrderListQuery);
        return orderDAO.countAdminOrders(query);
    }
    @Override public List<OrderBO> queryOrders(AdminOrderListQuery adminOrderListQuery) {
        AdminOrderDBPagerQuery query = orderConverter.query2dbQuery(adminOrderListQuery);
        List<OrderDO> orders=orderDAO.queryAdminOrders(query);
        return orderConverter.dos2bos(orders);
    }
    @Override public OrderBO getOrderById(Long id) {
        OrderDO order = orderDAO.getOrderById(id);
        return this.orderConverter.do2bo(order);
    }

    @Override public void saveOrder(OrderCreateParam param) {
        OrderDO orderDO = orderConverter.param2do(param);
        orderDAO.saveOrder(orderDO);
    }

    @Override public OrderBO getOrderByOrderNo(String no) {
        OrderDO orderDO=orderDAO.selectOrderByOrderNo(no);
        return orderConverter.do2bo(orderDO);
    }

    @Override public void closeOrder(OrderParam param) {
        OrderDO orderDO=orderConverter.param2do(param);
        orderDAO.updateOrderStatusByOrderNo(orderDO);
    }

    @Override public Long countOrders(FrontOrderListQuery query) {
        FrontOrderDBPagerQuery dbQuery=orderConverter.query2dbQuery(query);
        return orderDAO.countFrontOrders(dbQuery);
    }

    @Override public List<OrderBO> queryOrders(FrontOrderListQuery query) {
        FrontOrderDBPagerQuery dbQuery=orderConverter.query2dbQuery(query);
        List<OrderDO> dos = orderDAO.queryFrontOrders(dbQuery);
        return orderConverter.dos2bos(dos);
    }

    @Override public void updateStatus(OrderParam param) {
        OrderDO orderDO = orderConverter.param2do(param);
        orderDAO.updateOrderStatusByOrderNo(orderDO);
    }
}
